package com.ruibang.glass.portal.controller;

import com.ruibang.glass.common.domain.DictInfo;
import com.ruibang.glass.portal.constant.UserErrorCode;
import com.ruibang.glass.portal.domain.DictCondition;
import com.ruibang.glass.portal.entity.DictEntity;
import com.ruibang.glass.portal.service.DictService;
import com.teaming.cloud.framework2.common.pojo.PageResult;
import com.teaming.cloud.framework2.common.model.ResultBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @Auther: csz
 * @Date: 2018/8/23 14:21
 * @Description:
 */
@Slf4j
@Api(tags = "字典管理")
@RestController
@RequestMapping("/api/dict")
public class DictController {

    @Autowired
    DictService dictService;

    @ApiOperation(value = "保存字典", response = ResultBody.class)
    @PostMapping(value = "/save")
    public ResultBody<Void> saveAcDict(@RequestBody @Valid DictEntity dictEntity) {
        return dictService.saveDict(dictEntity);
    }

    @ApiOperation(value = "更新字典", response = ResultBody.class)
    @PostMapping(value = "/update")
    public ResultBody<Void> updateAcDict(@RequestBody DictEntity entity) {
        return dictService.updateDict(entity);
    }

    @ApiOperation(value = "删除字典", response = ResultBody.class)
    @GetMapping(value = "/delete/{id}")
    public ResultBody deleteDict(@PathVariable("id") Long id) {
        dictService.deleteDict(id);
        return ResultBody.success();
    }

    @ApiOperation(value = "根据类型名称获取字典信息", response = ResultBody.class)
    @GetMapping(value = "/getDictByTypeName/{typeName}")
    public ResultBody<List<DictEntity>> getDictByTypeName(@PathVariable("typeName") String typeName) {
        if (StringUtils.isBlank(typeName)) {
            return ResultBody.fail(UserErrorCode.PARAM_IS_EMPTY.getCode(), UserErrorCode.PARAM_IS_EMPTY.getMsg());
        }
        return ResultBody.success(dictService.getDictByTypeName(typeName));
    }

    @ApiOperation(value = "分页查询字典信息", response = ResultBody.class)
    @PostMapping(value = "/getAllDict")
    public ResultBody<PageResult<List<DictEntity>>> getAllDict(@RequestBody DictCondition condition) {
        return dictService.getAllDict(condition);
    }

    @ApiOperation(value = "根据Id获取字典值", response = ResultBody.class)
    @GetMapping(value = "/getDictById/{id}")
    public ResultBody<DictEntity> getDictById(@PathVariable("id") Long id) {
        return ResultBody.success(dictService.getById(id));
    }

    @ApiOperation(value = "通过groupName列表查询字典属性信息", response = ResultBody.class)
    @PostMapping(value = "/getDictByGroupNames")
    public ResultBody<List<DictEntity>> getDictByGroupNames(@RequestBody List<String> groupNames) {
        return ResultBody.success(dictService.getDictByGroupNames(groupNames));
    }

    @ApiOperation(value = "设置系统版本", response = ResultBody.class)
    @PostMapping(value = "/setSystemVersion")
    public ResultBody setSystemVersion(@RequestParam("version") String version) {
        dictService.setSystemVersion(version);
        return ResultBody.success();
    }

    @ApiOperation(value = "获取系统版本", response = ResultBody.class)
    @GetMapping(value = "/getSystemVersion")
    public ResultBody<String> getSystemVersion() {
        return ResultBody.success(dictService.getSystemVersion());
    }

    @ApiOperation(value = "设置APP更新地址", response = ResultBody.class)
    @PostMapping(value = "/setAppAddress")
    public ResultBody setAppAddress(@RequestParam("address") String address) {
        dictService.setAppAddress(address);
        return ResultBody.success();
    }

    @ApiOperation(value = "获取APP更新地址", response = ResultBody.class)
    @GetMapping(value = "/getAppAddress")
    public ResultBody<String> getAppAddress() {
        return ResultBody.success(dictService.getAppAddress());
    }

    @ApiOperation(value = "获取字典列表", response = ResultBody.class)
    @GetMapping(value = "/getDictList")
    public ResultBody getDictList() {
        return ResultBody.success(dictService.getDictList());
    }

    @ApiOperation(value = "根据字典代码获取字典值", response = ResultBody.class)
    @GetMapping(value = "/getValueByCode/{code}")
    public ResultBody<String> getValueByCode(@PathVariable("code") String code) {
        return ResultBody.success(dictService.getValueByCode(code));
    }

    @ApiOperation(value = "根据字典代码跟新字典值", response = ResultBody.class)
    @PostMapping(value = "/updateValueByCode")
    public ResultBody updateValueByCode(@RequestBody DictInfo dict) {
        dictService.updateValueByCode(dict);
        return ResultBody.success();
    }
}
